package com.tanhua.sso.controller;

import com.tanhua.sso.service.UserInfoService;
import com.tanhua.sso.vo.ErrorResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@RequestMapping("user")
@Slf4j
public class UserInfoController {
    @Autowired
   private UserInfoService userinfoService;

    /**
     * bao cun yong hu
     * @param param
     * @param token
     * @return
     */
    @PostMapping("loginReginfo")
    public ResponseEntity<Object> save(@RequestBody Map<String, String> param,
                                       @RequestHeader("Authorization") String token)
    {
        try {
            Boolean bool = userinfoService.saveUserInfo(param, token);
            if (bool) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        ErrorResult errorResult=ErrorResult.builder().errCode("000002").errMessage("保存用户信息失败！").build();
        return ResponseEntity.status(500).body(errorResult);
    }

    /**
     *
     * @param file
     * @param token
     * logo上传
     * @return
     */
    @PostMapping("loginReginfo/head")
    public ResponseEntity<Object> saveUserLogo(@RequestParam("headPhoto") MultipartFile file,
                                       @RequestHeader("Authorization") String token)
    {
        try {
            Boolean bool = userinfoService.saveimage(file,token);
            if (bool) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        ErrorResult errorResult=ErrorResult.builder().errCode("000002").errMessage("保存用户logo失败！").build();
        return ResponseEntity.status(500).body(errorResult);
    }

}
